<template>
  <div style="width: 100%;height:100%;">
    <!--商城设置 -->
    <div class="shopSet" style="width:70%;">
      <Form ref="formItem" :model="formItem" :label-width="150">
        <FormItem label="商家名称" prop="name">
            <Input v-model="formItem.name" placeholder="请输入商家名称"></Input>
        </FormItem>
        <FormItem label="商家Logo" style="text-align:left;" prop="avatar">
            <img :src="formItem.cover" alt="" @click="look($event)" class="smallImg" style="width: 50px;"/>
          <Upload action="https://shop.hx2lm.com/api/user/upload" :on-success="Success">
            <Button type="ghost" icon="ios-cloud-upload-outline" style="background:#19BE6B;color:#fff;">上传商家Logo</Button>
          </Upload>
        </FormItem>
        <FormItem label="商家相册" style="text-align:left;" prop="album">
            <div class="demo-upload-list" v-for="(item, index) in uploadList" :key="index">
              <img :src="item">
              <div class="demo-upload-list-cover">
                  <Icon type="ios-eye-outline" @click.native="handleView(item)"></Icon>
                  <Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
              </div>
            </div>
            <Upload
                ref="upload"
                :show-upload-list="false"
                :on-success="handleSuccess"
                :format="['jpg','jpeg','png']"
                :max-size="2048"
                :on-format-error="handleFormatError"
                :on-exceeded-size="handleMaxSize"
                :before-upload="handleBeforeUpload"
                multiple
                type="drag"
                action="https://shop.hx2lm.com/api/user/upload"
                style="display: inline-block;width:58px;">
                <div style="width: 58px;height:58px;line-height: 58px;">
                    <Icon type="camera" size="20"></Icon>
                </div>
            </Upload>
            <Modal title="查看图片" v-model="visible">
                <img :src="imgName " v-if="visible" style="width: 100%">
            </Modal>
        </FormItem>
        <FormItem label="商家地址" prop="address">
            <Input v-model="formItem.address" placeholder="请输入商家地址"></Input>
        </FormItem>
        <FormItem label="商家经纬度" >
            <Input v-model="formItem.lon" placeholder="请输入经度,如133.5456454"></Input>
            <p style="color:gray">请输入经度，如133.5456454</p>
            <Input v-model="formItem.lat" placeholder="请输入纬度,如33.5456454"></Input>
            <p style="color:gray">请输入纬度,如33.5456454</p>
        </FormItem>
        <FormItem label="商家电话" prop="phone">
            <Input v-model="formItem.phone" placeholder="请输入商家电话"></Input>
        </FormItem>
        <FormItem label="营业时间" prop="start_time">
          <div style="display:inline-block;">
            <TimePicker :value="formItem.start_at" @on-change="changeStar" format="HH:mm" placeholder="请选择开始营业时间"></TimePicker>
            <p style="color:gray">请选择开始营业时间</p>
          </div>
          <div style="display:inline-block;">
            <TimePicker :value="formItem.end_at" @on-change="changeEnd" format="HH:mm" placeholder="请选择结束营业时间"></TimePicker>
            <p style="color:gray">请选择结束营业时间</p>
          </div>
        </FormItem>
        <FormItem label="是否营业" prop="status" style="text-align:left;">
            <i-switch :value="formItem.status" size="large"  @on-change="switchChange">
              <span slot="open">是</span>
              <span slot="close">否</span>
            </i-switch>
        </FormItem>   
        <FormItem label="起送金额(元)" prop="startMoney">
            <Input v-model="formItem.take_price" placeholder="0.00"></Input>
        </FormItem>
        <!-- <FormItem label="配送费用(元)" prop="sendMoney">
            <Input v-model="formItem.delivery" placeholder="0.00"></Input>
        </FormItem> -->
        <FormItem label="配送距离(km)" prop="sendDistance">
            <Input v-model="formItem.take_distance" placeholder="0.00"></Input>
        </FormItem>
        <FormItem style="text-align:left;">
            <Button type="primary" @click="editShop1" class="addBtn" style="border-color:#19BE6B;font-size:16px;">确定</Button>
        </FormItem>
      </Form>
    </div>
  </div>
</template>
<script>
import { shop_info, editshopInfo } from "../../api/set/set.js";
import Base from "../../common/Base";
import { mapActions } from "vuex";
import Vue from "vue";
export default {
  mixins: [Base],
  data() {
    return {
      formItem: {},
      defaultList: [],
      imgName: "",
      visible: false,
      uploadList: [],
      status:null,
      start_at:'',
      end_at: ''
    };
  },
  methods: {
    ...mapActions(["checkSettingsIndex"]),
    //放大图片
    look(ev) {
      this.loadImg(ev.target.src);
    },
    //修改开始时间
    changeStar(val1) {
      // console.log(val1)
      this.formItem.start_at = val1;
    },
    changeEnd(val2) {
      // console.log(val2)
      this.formItem.end_at = val2;
    },
    // 切换营业状态
    switchChange(value) {
      // console.log(value)
      this.$Message.info("设置成功 !");
      this.formItem.status = value;
    },
    // 预览图片
    handleView(name) {
      this.imgName = name;
      this.visible = true;
    },
    // 删除图片
    handleRemove(file) {
      this.uploadList.splice(this.uploadList.indexOf(file), 1);
    },
    // 上传成功
    handleSuccess(res, file) {
      this.uploadList.push(res.data.url);
    },
    // 文件格式验证失败时的钩子
    handleFormatError(file) {
      this.$Notice.warning({
        title: "文件格式不正确",
        desc:
          "该文件 " +
          file.name +
          " 类型不对, 请选择 jpg 或 png."
      });
    },
    // 文件超出指定大小限制时的钩子
    handleMaxSize(file) {
      this.$Notice.warning({
        title: "文件超过限制",
        desc: "文件  " + file.name + "太大, 不能超过 2M."
      });
    },
    // 上传文件之前的钩子，参数为上传的文件
    handleBeforeUpload() {
      const check = this.uploadList.length < 5;
      if (!check) {
        this.$Notice.warning({
          title: "最多只能上传5张图片"
        });
      }
      return check;
    },

    Success(res) {
      this.formItem.cover = res.data.url;
    },

    //确认编辑
    editShop1() {
      var that = this
      if (that.formItem.name != '') {
          if (that.uploadList.length >= 1) {
            if (that.formItem.cover != '') {
              if (that.formItem.address != '') {
                if (that.isNumber(that.formItem.lon) && that.isNumber(that.formItem.lat)) {
                  if (that.IsMobile(that.formItem.phone)) {
                    if (that.isNumber(that.formItem.take_price)) {
                      if (that.isNumber(that.formItem.take_distance)) {
                        if (that.formItem.start_at != '' && that.formItem.end_at != '') {
                          if (that.formItem.status == true) {
                            that.status = 1
                          } else {
                            that.status = 2
                          }
                          editshopInfo({
                            name:that.formItem.name,
                            cover: that.formItem.cover,
                            album: JSON.stringify(that.uploadList),
                            address: that.formItem.address,
                            lon:that.formItem.lon,
                            lat:that.formItem.lat,
                            phone:that.formItem.phone,
                            start_at:that.formItem.start_at,
                            end_at: that.formItem.end_at,
                            status: that.status,
                            take_distance: that.formItem.take_distance,
                            take_price: that.formItem.take_price
                          }).then(res => {

                            // console.log(res)
                            if (res.code == 0) {
                              this.$Message.success("修改成功！");
                              that.getData();
                            }
                          });
                        } else {
                            this.$Message.error("请选择营业时间");
                        }
                      } else {
                          this.$Message.error("请输入配送距离");
                      } 
                    } else {
                        this.$Message.error("请输入起送金额");
                    } 
                  } else {
                      this.$Message.error("请输入联系方式");
                  }
                } else {
                    this.$Message.error("请输入商家经纬度");
                }
              } else {
                  this.$Message.error("请输入商家地址");
              }
            } else {
                this.$Message.error("请上传商家logo");
            }
          } else {
              this.$Message.error("请上传商家相册");
          }
      } else {
          this.$Message.error("请输入商家名称");
      }
    },
    //获取数据
    getData() {
      shop_info({}).then(
        res => {
          // console.log(res)
          if (res.data.status == 1) {
            res.data.status = true
          } else {
             res.data.status = false
          }
          this.uploadList = res.data.album;
          this.formItem = res.data;
          // console.log(res.data)
        }
      );
    }
  },
  beforeCreate() {
    this.$store.dispatch("checkSettingsIndex", "shopSet");
  },
  mounted() {
    this.getData();
  }
};
</script>
<style lang="scss" scoped>
@import "../../css/common.scss";
.shopSet {
  padding-top: 30px;
  padding-bottom: 50px;
}
.demo-upload-list {
  display: inline-block;
  width: 60px;
  height: 60px;
  text-align: center;
  line-height: 60px;
  border: 1px solid transparent;
  border-radius: 4px;
  overflow: hidden;
  background: #fff;
  position: relative;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
  margin-right: 4px;
}
.demo-upload-list img {
  width: 100%;
  height: 100%;
}
.demo-upload-list-cover {
  display: none;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.6);
}
.demo-upload-list:hover .demo-upload-list-cover {
  display: block;
}
.demo-upload-list-cover i {
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  margin: 0 2px;
}
</style>